#!/bin/bash
# description:　WebReader Auto Install Server
# version:	1.0
# author:	jinping_huang
# contact:	jinping_huang@foxitsoftware.com
#

# Add epel repo
if [ ! -e /etc/yum.repos.d/epel.repo ]; then
    echo "Installing EPEL repo..."
    sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
fi

# Install Apache (and any needed extensions).
pkgs=( git httpd )
for i in "${pkgs[@]}"
do
    if rpm -qa | grep -q ^$i; then
        echo "$i already installed"
    else
        echo "Installing $i and dependencies..."
        sudo yum install -y $i
    fi
done

# Set ServerName for apache
if [ ! -e "/etc/httpd/conf.d/servername.conf" ]; then
    echo "Setting servername in for apache"
    sudo echo "ServerName local.api.pdf2pdf.com" >> /etc/httpd/conf.d/servername.conf
fi

# Make sure Apache is configured to start automatically and is running.
echo "Ensuring httpd is running and enabled at startup..."
sudo chkconfig httpd on

# Restart Apache.
echo "Restarting apache"
sudo service httpd restart

#Setup base infrastructure for cloud service
sudo chmod 0755 /vagrant/vagrant/baselayer.sh
sudo /vagrant/vagrant/baselayer.sh
retVal=$?
if [ $retVal -ne 0 ]
then
	echo "baselayer.sh.........[ERROR]"
	exit $retVal
fi
#Setup cloud service
sudo chmod 0755 /vagrant/vagrant/servicelayer.sh
sudo /vagrant/vagrant/servicelayer.sh
retVal=$?
if [ $retVal -ne 0 ]
then
	echo "servicelayer.sh.........[ERROR]"
	exit $retVal
fi
#Create WebReader
sudo chmod 0755 /vagrant/vagrant/pdf2pdf.sh
sudo /vagrant/vagrant/pdf2pdf.sh
retVal=$?
if [ $retVal -ne 0 ]
then
	echo "pdf2pdf.sh.........[ERROR]"
	exit $retVal
fi

echo "Start.............[SUCCESS]"
